<style>
</style>
<template>
    <div id="user">
        <el-form  :model="form" label-width="80px" :rules="rules" ref="ruleForm" class="demo-ruleForm">
            <el-form-item label="用户昵称" prop="username" >
                <el-input v-model="form.userName" ></el-input>
            </el-form-item>

            <el-form-item label="密码" prop="pass">
                <el-input type="password" v-model="form.password" autocomplete="off" ></el-input>
            </el-form-item>


            <el-form-item label="用户姓名" prop="name">
                <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item label="部门" prop="department">
                <el-select v-model="form.departmentId" placeholder="请选择用户部门">
                    <el-option label="燃料管理部"  value="1"></el-option>
                    <el-option label="监察部"  value="2"></el-option>
                    <el-option label="信息管理部"  value="3"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="联系方式">
                <el-radio-group v-model="form.intelephone">
                    <el-radio label="电话"></el-radio>
                    <el-radio label="邮箱"></el-radio>
                </el-radio-group>
            </el-form-item>

            <el-form-item v-bind:label="'用户'+form.intelephone" prop="telNumber">
                <el-input v-model="form.telNumber"></el-input>
            </el-form-item>

            <el-form-item label="用户角色" prop="actor">
                <el-radio-group v-model="form.actorId">
                    <el-radio label="1" >平台超级管理员</el-radio>
                    <el-radio label="2" >注册用户审核人员</el-radio>
                    <el-radio label="3" >交易审核人员</el-radio>
                    <el-radio label="4" >资讯审核人员</el-radio>
                    <el-radio label="5" >资讯维护人员</el-radio>
                    <el-radio label="6" >资讯编辑人员</el-radio>
                    <el-radio label="7" >交易发起人</el-radio>
                    <el-radio label="8" >财务人员</el-radio>
                </el-radio-group>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="onSubmit">立即创建</el-button>
                <el-button>取消</el-button>
            </el-form-item>
        </el-form>
        {{form.actorId}}<hr>
        {{form.departmentId}}
    </div>

</template>

<script>
    import axios from "axios"
    export default {
        name: "manager",
        data() {
            const PassReg = /^(?=.*\d)(?=.*[a-zA-Z])(?=.*[-+*#])[\da-zA-Z-+*#]{1,}$/

            const emailReg = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/

            const phoneReg = /^[1]([3-9])[0-9]{9}$/

            var validateUsername = (rule, value, callback) => {
                if (this.form.userName === '') {
                    callback(new Error('请输入用户名'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass = (rule, value, callback) => {
                if (this.form.password === '') {
                    callback(new Error('请输入密码'));
                } else if(PassReg.test(this.form.password)){
                    callback();
                }else{
                    callback(new Error('密码必须包含字母，数字和-+*#'));
                }
            };
            var validateTelNumber = (rule, value, callback) => {
                console.log(this.form.telNumber)
                if (this.form.telNumber === '') {
                    callback(new Error('请输入联系方式'));
                } else if(emailReg.test(this.form.telNumber) || phoneReg.test(this.form.telNumber)){
                    callback();
                }else{
                    callback(new Error('请输入邮箱号或手机号'));
                }
            };
            return {

                rules:{
                    username:[
                        {required: true, validator: validateUsername,  trigger: 'blur'}
                    ],
                    pass: [
                        { required: true,validator: validatePass, trigger: 'blur' }
                    ],
                    name:[
                        {required: true, message: '请输入姓名', trigger: 'blur'}
                    ],
                    department:[
                        {required: true, message: '请选择部门', trigger: 'blur'}
                    ],
                    telNumber:[
                        {required: true,validator: validateTelNumber, trigger: 'blur'}
                    ],
                    actor:[
                        {required: true, message: '请选择角色', trigger: 'blur'}
                    ]
                },
                user: JSON.parse(window.sessionStorage.getItem('userDate')),
                form: {
                    name: '',
                    userName: '',
                    password: '',
                    telNumber: '',
                    intelephone: '电话',
                    actorId: '',
                    departmentId: ''
                },
                roleList: [],
                departmentList: []
            }
        },
        methods: {
            onSubmit() {
                if(this.user.actor!=="平台超级管理员"){
                    this.$message({
                        showClose: true,
                        message: '无权限操作！',
                        type: 'error'
                    });
                    return false;
                }
                var token = this.user.token
                this.postRequest('/addUser?token='+token, this.form
                ).then(response=> {
                    console.log(response.data)
                    this.$message({
                        message: '创建成功',
                        type: 'success'
                    });
                })
                    .catch(function (error) {
                        console.log(error);
                    });
            }
        }
    }
</script>


<style scoped>

</style>
